﻿using System;
using System.Web.Mvc;
using Lilium.Web.Mvc.State;

namespace Lilium.Web.Mvc.EntityFilters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public sealed class PagerAttribute : EntityFilterAttribute
    {
        public PagerAttribute(Type entityType)
            : base(Pager.GetPagerType(entityType))
        {
        }

        #region public int PageSize

        private int m_PageSize = 25;

        public int PageSize
        {
            get { return m_PageSize; }
            set { m_PageSize = value; }
        }

        #endregion

        protected override IEntityFilter CreateFilter(ActionExecutingContext filterContext)
        {
            var pager = (IPager)filterContext.Controller.GetService(this.FilterType, true);

            var stateSetvice = filterContext.Controller.GetService<IStateService>(false);
            if ((stateSetvice == null) || (!stateSetvice.PropertyLoaded(pager, "PageSize")))
                pager.PageSize = PageSize;

            return pager;
        }
    }
}